package com.cq.hd.order.convert;

import com.cq.hd.common.enums.biz.GroupBuyOrderItemStatusEnum;
import com.cq.hd.common.enums.biz.GroupBuyStatusEnum;
import com.cq.hd.member.api.vo.AppUserVo;
import com.cq.hd.order.api.dto.CreateGroupBuyOrderDto;
import com.cq.hd.order.api.dto.CreateZeroGroupBuyOrderDto;
import com.cq.hd.order.po.TbGroupBuyOrderItemPo;
import com.cq.hd.order.po.TbGroupBuyOrderPo;
import com.cq.hd.product.api.vo.ActiveGroupBuyVo;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Random;

/**
 * @author Justin.Chen
 * @since 2024/09/22 18:51
 */
public class GroupBuyOrderConvert {

    public static TbGroupBuyOrderItemPo wrapGroupBuyOrderItemPo(
            TbGroupBuyOrderPo groupBuyOrderPo, CreateGroupBuyOrderDto createGroupBuyOrderDto, AppUserVo appUserVo) {
        TbGroupBuyOrderItemPo groupBuyOrderItemPo = new TbGroupBuyOrderItemPo();
        groupBuyOrderItemPo.setGroupBuyOrderId(groupBuyOrderPo.getId());
        groupBuyOrderItemPo.setOrderId(createGroupBuyOrderDto.getOrderId());
        groupBuyOrderItemPo.setOrderNo(createGroupBuyOrderDto.getOrderNo());

        groupBuyOrderItemPo.setActiveId(createGroupBuyOrderDto.getActiveId());
        groupBuyOrderItemPo.setActiveTitle(createGroupBuyOrderDto.getActiveTitle());

        groupBuyOrderItemPo.setUserId(appUserVo.getId());
        groupBuyOrderItemPo.setAvatar(appUserVo.getAvatar());
        groupBuyOrderItemPo.setNickName(appUserVo.getNickName());
        groupBuyOrderItemPo.setMobile(appUserVo.getMobile());
        // 是否团长
        groupBuyOrderItemPo.setGroupLead(Objects.equals(groupBuyOrderPo.getLeadUserId(), appUserVo.getId()) ? 1 : 0);

        groupBuyOrderItemPo.setStatus(GroupBuyOrderItemStatusEnum.WAIT_PAY.getValue());
        LocalDateTime now = LocalDateTime.now();
        groupBuyOrderItemPo.setCreateTime(now);
        groupBuyOrderItemPo.setUpdateTime(now);

        return groupBuyOrderItemPo;
    }

    public static TbGroupBuyOrderPo wrapGroupBuyOrderPo(
            CreateGroupBuyOrderDto createGroupBuyOrderDto, ActiveGroupBuyVo activeGroupBuyVo, AppUserVo appUserVo) {
        TbGroupBuyOrderPo groupBuyOrderPo = new TbGroupBuyOrderPo();
        groupBuyOrderPo.setActiveId(createGroupBuyOrderDto.getActiveId());
        groupBuyOrderPo.setGroupBuyActiveId(activeGroupBuyVo.getId());
        groupBuyOrderPo.setLeadUserId(createGroupBuyOrderDto.getUserId());
        groupBuyOrderPo.setLeadNickName(appUserVo.getNickName());
        groupBuyOrderPo.setLeadMobile(appUserVo.getMobile());
        LocalDateTime expiredTime = LocalDateTime.now().plusHours(activeGroupBuyVo.getGroupDuration());
        groupBuyOrderPo.setExpiredTime(expiredTime);
        groupBuyOrderPo.setGroupNum(activeGroupBuyVo.getGroupNum());
        groupBuyOrderPo.setCurrentNum(1);
        groupBuyOrderPo.setVirtualGroup(activeGroupBuyVo.getVirtualGroup());
        groupBuyOrderPo.setRewardType(createGroupBuyOrderDto.getRewardType());
        groupBuyOrderPo.setDirectJoinEnabled(activeGroupBuyVo.getDirectJoinEnabled());
        groupBuyOrderPo.setStatus(GroupBuyStatusEnum.INACTIVE.getValue());
        groupBuyOrderPo.setFreeType(activeGroupBuyVo.getFreeType());
        LocalDateTime now = LocalDateTime.now();
        groupBuyOrderPo.setCreateTime(now);
        groupBuyOrderPo.setUpdateTime(now);
        return groupBuyOrderPo;
    }

    // 零元开团，不生成tb_order
    public static TbGroupBuyOrderPo wrapZeroGroupBuyOrderPo(
            CreateZeroGroupBuyOrderDto createZeroGroupBuyOrderDto, ActiveGroupBuyVo activeGroupBuyVo, AppUserVo appUserVo) {
        TbGroupBuyOrderPo groupBuyOrderPo = new TbGroupBuyOrderPo();
        groupBuyOrderPo.setActiveId(createZeroGroupBuyOrderDto.getActiveId());
        groupBuyOrderPo.setGroupBuyActiveId(activeGroupBuyVo.getId());
        groupBuyOrderPo.setLeadUserId(createZeroGroupBuyOrderDto.getUserId());
        groupBuyOrderPo.setLeadNickName(appUserVo.getNickName());
        groupBuyOrderPo.setLeadMobile(appUserVo.getMobile());
        LocalDateTime expiredTime = LocalDateTime.now().plusHours(activeGroupBuyVo.getGroupDuration());
        groupBuyOrderPo.setExpiredTime(expiredTime);
        groupBuyOrderPo.setGroupNum(activeGroupBuyVo.getGroupNum());
        groupBuyOrderPo.setCurrentNum(1);
        groupBuyOrderPo.setVirtualGroup(activeGroupBuyVo.getVirtualGroup());
        groupBuyOrderPo.setRewardType(createZeroGroupBuyOrderDto.getRewardType());
        groupBuyOrderPo.setDirectJoinEnabled(activeGroupBuyVo.getDirectJoinEnabled());
        groupBuyOrderPo.setStatus(GroupBuyStatusEnum.PENDING.getValue());
        groupBuyOrderPo.setFreeType(activeGroupBuyVo.getFreeType());
        LocalDateTime now = LocalDateTime.now();
        groupBuyOrderPo.setCreateTime(now);
        groupBuyOrderPo.setUpdateTime(now);
        return groupBuyOrderPo;
    }

    public static TbGroupBuyOrderItemPo wrapZeroGroupBuyOrderItemPo(
            TbGroupBuyOrderPo groupBuyOrderPo, CreateZeroGroupBuyOrderDto createZeroGroupBuyOrderDto, AppUserVo appUserVo) {
        TbGroupBuyOrderItemPo groupBuyOrderItemPo = new TbGroupBuyOrderItemPo();
        groupBuyOrderItemPo.setGroupBuyOrderId(groupBuyOrderPo.getId());

        groupBuyOrderItemPo.setActiveId(createZeroGroupBuyOrderDto.getActiveId());
        groupBuyOrderItemPo.setActiveTitle(createZeroGroupBuyOrderDto.getActiveTitle());

        groupBuyOrderItemPo.setUserId(appUserVo.getId());
        groupBuyOrderItemPo.setAvatar(appUserVo.getAvatar());
        groupBuyOrderItemPo.setNickName(appUserVo.getNickName());
        groupBuyOrderItemPo.setMobile(appUserVo.getMobile());
        // 是否团长
        groupBuyOrderItemPo.setGroupLead(Objects.equals(groupBuyOrderPo.getLeadUserId(), appUserVo.getId()) ? 1 : 0);

        groupBuyOrderItemPo.setStatus(GroupBuyOrderItemStatusEnum.PAID.getValue());
        LocalDateTime now = LocalDateTime.now();
        groupBuyOrderItemPo.setCreateTime(now);
        groupBuyOrderItemPo.setUpdateTime(now);

        return groupBuyOrderItemPo;
    }

    public static TbGroupBuyOrderItemPo wrapVirtualGroupBuyOrderItemPo(Long groupOrderId, String username) {
        TbGroupBuyOrderItemPo groupBuyOrderItemPo = new TbGroupBuyOrderItemPo();
        groupBuyOrderItemPo.setGroupBuyOrderId(groupOrderId);

        groupBuyOrderItemPo.setUserId(0L);
        groupBuyOrderItemPo.setAvatar(getRandomAvatar());
        groupBuyOrderItemPo.setNickName(username);
        groupBuyOrderItemPo.setMobile("");
        // 是否团长
        groupBuyOrderItemPo.setGroupLead(0);

        groupBuyOrderItemPo.setStatus(GroupBuyOrderItemStatusEnum.PAID.getValue());
        LocalDateTime now = LocalDateTime.now();
        groupBuyOrderItemPo.setCreateTime(now);
        groupBuyOrderItemPo.setUpdateTime(now);

        return groupBuyOrderItemPo;
    }

    private static String getRandomAvatar() {
        String[] avatars = {
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010152031.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010152004.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151905.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151831.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151739.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151657.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151645.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151620.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151536.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151438.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151402.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151324.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151256.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151217.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151126.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010151047.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010150955.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010150932.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010150904.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010150851.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010150821.png",
                "https://huodtg.oss-cn-hangzhou.aliyuncs.com/hd/head/20241010150758.png"
        };

        // 随机获取一个头像
        int random = new Random().nextInt(avatars.length);
        return avatars[random];
    }

}
